<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\PlanBill;

class InventoryController extends Controller
{
	public function actionIndex()
	{	
		$query = new \yii\db\Query();

		$displayAll = Yii::$app->request->get("displayAll",0);

		if ($displayAll == 1){
			$list = $query->select("*")->from("JXC_INVENTORY")->orderBy("INVENTORYID DESC")->all();
		}else{
			$list = $query->select("*")->from("JXC_INVENTORY")->orderBy("INVENTORYID DESC")->limit(DEFAULT_LIMIT)->all();
		}

		$sumInventory = 0;
		foreach ($list as  $value) {
			$sumInventory += $value['TOTALNUMBER'];
		}
		return $this->render("index",['list' => $list,'sumInventory'=>$sumInventory]);
	}

	private function generateQueryString($fieldName){
		if (empty($fieldName))
			return "";
		$getFieldNameRequest = $_GET[$fieldName];
		if (empty($getFieldNameRequest))
			return "";
		else
			return $fieldName." like '%".$getFieldNameRequest."%'";
	}

	public function actionSearch()
	{	
		$orderunit = $_GET['orderunit'];
		$receivingunit = $_GET['receivingunit'];
		$orderdate = $_GET['orderdate'];
		$orderdate_end = $_GET['orderdate_end'];
		$deliverydate = $_GET['deliverydate'];
		$deliverydate_end = $_GET['deliverydate_end'];

		$query = new \yii\db\Query();

		$query->select("*")
			  ->from("JXC_INVENTORY");


		$queryString = $this->generateQueryString("productname");
		if (!empty($queryString)){
			$query->andWhere($queryString);
		}
		$queryString = $this->generateQueryString("drawingno");
		if (!empty($queryString)){
			$query->andWhere($queryString);
		}
		$queryString = $this->generateQueryString("specification");
		if (!empty($queryString)){
			$query->andWhere($queryString);
		}
		$queryString = $this->generateQueryString("position");
		if(!empty($queryString)){
			$query->andWhere($queryString);
		}

		if(!($orderunit == "" && $receivingunit == "" && $orderdate == "" && $deliverydate == "" && $orderdate_end == "" && $deliverydate_end == ""))
		{
			$plannumList = PlanBill::getPlannum($orderunit,$receivingunit,$orderdate,$orderdate_end,$deliverydate,$deliverydate_end);
			$query->andWhere("PLANNUM in(".$plannumList.")");
		}
		
		$list = $query->all();

		$sumInventory = 0;
		foreach ($list as  $value) {
			$sumInventory += $value['TOTALNUMBER'];
		}
		return $this->render("index",['list' => $list,'sumInventory'=>$sumInventory]);
	}

	public function actionDetail()
	{
		$inventoryid = $_GET['id'];

		$query = new \yii\db\Query();
		
		$query->select("*")
			  ->from("JXC_INVENTORY")
			  ->where('inventoryid ='. $inventoryid);

		$inventory = $query->one();

		$retArr = array();
		$retArr[] = ['label' => '序号' , 'value' => $inventory['INVENTORYID']];
		$retArr[] = ['label' => '产品名称' , 'value' => $inventory['PRODUCTNAME']];
		$retArr[] = ['label' => '产品图号' , 'value' => $inventory['DRAWINGNO']];
		$retArr[] = ['label' => '规格型号' , 'value' => $inventory['SPECIFICATION']];
		$retArr[] = ['label' => '批次号' , 'value' => $inventory['BATCHNO']];
		$retArr[] = ['label' => '状态' , 'value' => $inventory['STATUS']];
		$retArr[] = ['label' => '可用库存数' , 'value' => $inventory['TOTALNUMBER']];
		$retArr[] = ['label' => '产品过期时间' , 'value' => $inventory['EXPIREDATE']];
		$retArr[] = ['label' => '存储位置' , 'value' => $inventory['POSITION']];
		$retArr[] = ['label' => '存储仓库号码' , 'value' => $inventory['DEPOSITORY']];
		$retArr[] = ['label' => '制单人' , 'value' => $inventory['CRUSER']];
		$retArr[] = ['label' => '制单时间' , 'value' => $inventory['CRTIME']];
		$retArr[] = ['label' => '生产单号' , 'value' => $inventory['PLANNUM']];
		$retArr[] = ['label' => '是否为代储库存' , 'value' => $inventory['ISRESERVE']];

		return json_encode($retArr);
	} 
}